<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class StationModel extends BaseModel
{
    /**ruan shan chu deleted_at 软删除 */
    //use SoftDeletes;

    /**
     * Undocumented variable
     *
     * @var string
     */
    protected $table = 'station';
    protected $dates = ['deleted_at'];

    protected $fillable = [
        'station_id',
        'compay_id',
        'dev_id',
        'station_name',
        'dlbz_type',
        'share_type',
        'province',
        'city',
        'county',
        'station_adds',
        'station_tel',
        'ser_tel',
        'parking_space',
        'station_type',
        'station_status',
        'construction_site_type',
        'open_all_day',
        'longitude',
        'latitude',
        'car_type_note',
        'low_fei_lv',
        'cd_fei_lv',
        'ser_fei_lv',
        'free_parking',
        'parking_fei',
        'par_fees',
        'pay_type',
        'par_fees_type',
        'yu_yue',
        'toilet',
        'store',
        'restaurant',
        'lounge',
        'canopy',
        'car_washing',
        'barrier_gate',
        'ground_lock',
        'price_mod_id',
        'note_bak',
        'created_by'
    ];

    /**
     * 表明模型是否应该被打上时间戳
     *
     * @var bool
     */
    public $timestamps = true;


    /**
     * Undocumented function
     *
     * @param [type] $pageSize
     * @param [type] $page
     * @param [type] $map
     * @param [type] $sort
     * @return void
     */
    public static function GetStationList($pageSize, $page, $map, $sort)
    {
        $stationList = self::select('*')
            ->where($map)
            ->orderBy($sort[0], $sort[1])
            ->paginate($pageSize, [], 'page', $page)->toArray();

        return $stationList;
    }

    /**
     * 经纬度范围内充电站列表
     *
     * @param [type] $pageSize
     * @param [type] $page
     * @param [type] $map
     * @param [type] $sort
     * @return void
     */
    public static function GetLatLngStationList($pageSize, $page, $map, $sort, $latArr, $lngArr, $keyWord, $latitude, $id)
    {
        $stationList = self::select('*')->where($map);
        if ($id) {
            $stationList = $stationList->where('id', $id);
        }

        if (!$keyWord && $latitude != '') {
            $stationList = $stationList->whereBetween('latitude', $latArr);
            $stationList = $stationList->whereBetween('longitude', $lngArr);
        }
        $stationList = $stationList->where('station_name', 'like', '%' . $keyWord . '%');
        $stationList = $stationList->orderBy($sort[0], $sort[1]);
        $stationList = $stationList->paginate($pageSize, [], 'page', $page);
        $stationList = $stationList->toArray();
        return $stationList;
    }



    /**
     * 添加或编辑充电站
     *
     * @param [type] $data
     * @return void
     */
    public static function AddOrEditStation($data)
    {
        try {
            $id = $data['id'];
            unset($data['id']);
            if (!empty($id)) {
                $res = self::where('id', $id)->update($data);
            } else {
                $res = self::create($data);
            }
        } catch (\Exception $e) {
            return self::result('', 500, $e->getMessage());
        }
    }



    /**
     * 下拉框格式
     *[{"id":12,"name":"haidianzhan","status":0},{"id":12,"name":"bei jing zhan","status":1}]
     * @param array $data
     * @return void
     */
    public static function CateList($data = [])
    {
        if (!empty($data)) return false;
        foreach ($data as $key => $val) {
            if ($val['id'] && $val['station_name']) {
                $arr[] = [
                    'id' => $val['id'],
                    'station_name' => $val['station_name'],
                    'status' => $val['station_status'] == 0 ? 0 : 1,
                ];
            }
        }
        return $arr;
    }


    /**
     * 删除充电站通过ids
     *
     * @param [type] $ids
     * @return void
     */
    public static function DelStationByMap($ids)
    {
        try {
            self::BeginTrans();
            $res = self::destroy($ids);
            self::CommitTrans();
            if ($res) {
                return self::result('', 200, 'delete station success');
            }
            return self::result("", 500, 'delete station failed');
        } catch (\Exception $e) {
            return self::result("", 500, $e->getMessage());
        }
    }


    /**
     * 查找充电桩信息by map
     *
     * @param [type] $map
     * @return void
     */
    public static function GetStationInfo($map)
    {
        $res = self::where($map)->first();
        return $res;
    }
}
